package com.scqkxx.gamebox.activity.game.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.scqkxx.gamebox.R;
import com.scqkxx.gamebox.activity.activities.utils.ActivityUtil;
import com.scqkxx.gamebox.activity.activities.utils.GlideUtils;
import com.scqkxx.gamebox.activity.myself.LoginActivity;
import com.scqkxx.gamebox.activity.webview.web.WebViewActivity;
import com.scqkxx.gamebox.activity.webview.web.model.WebViewConfig;
import com.scqkxx.gamebox.utils.StringUtIls;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2018/8/13. 09点55分
 * name : ts
 * 用途 ：游戏页面的适配器
 */

public class GameGridAdapter extends BaseAdapter {
    private Context context=null;
    private  List<Map<String , Object>> data= null;

    private class Holder{
        ImageView ivGame;
        TextView tvGameName;
        TextView tvGameContent;
        LinearLayout linGame;
    }
    //构造方法
    public GameGridAdapter(Context context,  List<Map<String , Object>> data ) {
        this.context = context;
        this.data = data;
    }


    @Override
    public int getCount() {
        return data.size();

    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        Holder holder;
        if(view==null){
            view= LayoutInflater.from(context).inflate(R.layout.item_game,null);
            holder=new Holder();
            holder.ivGame=view.findViewById(R.id.iv_game);
            holder.tvGameName=view.findViewById(R.id.tv_game_name);
            holder.tvGameContent=view.findViewById(R.id.tv_game_content);
            holder.linGame = view.findViewById( R.id.lin_game );
            view.setTag(holder);
        }else{
            holder=(Holder) view.getTag();
        }
        HashMap<String ,Object> item = (HashMap<String, Object>) data.get(position);
        GlideUtils.setImageeCircle(context.getApplicationContext(), item.get("gameLogo")+"" , holder.ivGame );
        holder.tvGameName.setText(StringUtIls.isEmpty((String) item.get("gameName")));
        holder.tvGameContent.setText(StringUtIls.isEmpty((String) item.get("gameDesc")));
        holder.linGame.setOnClickListener( v -> {
            WebViewConfig webViewConfig = new WebViewConfig(item.get("gameUrl") +"" );
            webViewConfig.isShowProgressBar(true);
            ActivityUtil.switchTo(context, WebViewActivity.getIntent( context , webViewConfig ));
//            ActivityUtil.switchTo(context, LoginActivity.class);
        });
        return view;
    }
}
